{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "assert 1== 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "ename": "AssertionError",
     "evalue": "assertion is wrong",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAssertionError\u001b[0m                            Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[6], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m \u001b[38;5;241m1\u001b[39m \u001b[38;5;241m==\u001b[39m\u001b[38;5;241m2\u001b[39m,\u001b[38;5;124m'\u001b[39m\u001b[38;5;124massertion is wrong\u001b[39m\u001b[38;5;124m'\u001b[39m\n",
      "\u001b[1;31mAssertionError\u001b[0m: assertion is wrong"
     ]
    }
   ],
   "source": [
    "assert 1 ==2,'assertion is wrong'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "if __debug__:\n",
    "    if not 1== 1: raise AssertionError"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      ".\n",
      "----------------------------------------------------------------------\n",
      "Ran 1 test in 0.001s\n",
      "\n",
      "OK\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "........\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<unittest.main.TestProgram at 0x1c54a06bd90>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# unit test单元测试\n",
    "import unittest\n",
    "def sort(arr):\n",
    "    l = len(arr)\n",
    "    for i in range(0,l):\n",
    "        for j in range(i,l):\n",
    "            if arr[i] > arr[j]:\n",
    "                tmp = arr[i]\n",
    "                arr[i] = arr[j]\n",
    "                arr[j] = tmp\n",
    "\n",
    "class TestSort(unittest.TestCase):\n",
    "    def test_sort(self):\n",
    "        arr = [3,4,1,5,6]\n",
    "        sort(arr=arr)\n",
    "        print('........')\n",
    "        self.assertEqual(arr,[1,3,4,5,6])\n",
    "# if __name__ == '__main__':\n",
    "unittest.main(argv=['first-arg-is-ignored'], exit=False)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      ".\n",
      "----------------------------------------------------------------------\n",
      "Ran 1 test in 0.001s\n",
      "\n",
      "OK\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "........\n"
     ]
    }
   ],
   "source": [
    "# mock用法\n",
    "import unittest\n",
    "from unittest.mock import MagicMock\n",
    "class A(unittest.TestCase):\n",
    "    def m1(self):\n",
    "        val = self.m2()\n",
    "        self.m3(val)\n",
    "    def m2(self):\n",
    "        pass\n",
    "    def m3(self,val):\n",
    "        pass\n",
    "def test_m1(self):\n",
    "    a = A()\n",
    "    a.m2 = MagicMock(return_value=\"custom_val\")\n",
    "    a.m3 = MagicMock()\n",
    "    a.m1()\n",
    "    #self.assertTrue(a.m2.called)\n",
    "    a.m3.assert_called_with(\"custom_val222\")\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    unittest.main(argv=['first-arg-is-ignored'],exit=False)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from unittest.mock import MagicMock\n",
    "def side_effect(arg):\n",
    "    if arg < 0:\n",
    "        return 1\n",
    "    else:\n",
    "        return 2\n",
    "mock = MagicMock()\n",
    "mock.side_effect = side_effect\n",
    "mock(-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "Need a valid target to patch. You supplied: 'sort'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "File \u001b[1;32mc:\\Users\\18027\\AppData\\Local\\Programs\\Python\\Python310\\lib\\unittest\\mock.py:1604\u001b[0m, in \u001b[0;36m_get_target\u001b[1;34m(target)\u001b[0m\n\u001b[0;32m   1603\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m-> 1604\u001b[0m     target, attribute \u001b[38;5;241m=\u001b[39m target\u001b[38;5;241m.\u001b[39mrsplit(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m.\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;241m1\u001b[39m)\n\u001b[0;32m   1605\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m (\u001b[38;5;167;01mTypeError\u001b[39;00m, \u001b[38;5;167;01mValueError\u001b[39;00m, \u001b[38;5;167;01mAttributeError\u001b[39;00m):\n",
      "\u001b[1;31mValueError\u001b[0m: not enough values to unpack (expected 2, got 1)",
      "\nDuring handling of the above exception, another exception occurred:\n",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[20], line 11\u001b[0m\n\u001b[0;32m      9\u001b[0m                 arr[i] \u001b[38;5;241m=\u001b[39m arr[j]\n\u001b[0;32m     10\u001b[0m                 arr[j] \u001b[38;5;241m=\u001b[39m tmp\n\u001b[1;32m---> 11\u001b[0m \u001b[38;5;129m@patch\u001b[39m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43msort\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[0;32m     12\u001b[0m \u001b[38;5;28;01mclass\u001b[39;00m \u001b[38;5;21;01mTestSort\u001b[39;00m(\u001b[38;5;28mself\u001b[39m,mock_sort):\n\u001b[0;32m     13\u001b[0m     \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mtest_sort\u001b[39m(\u001b[38;5;28mself\u001b[39m):\n\u001b[0;32m     14\u001b[0m         arr \u001b[38;5;241m=\u001b[39m [\u001b[38;5;241m3\u001b[39m,\u001b[38;5;241m4\u001b[39m,\u001b[38;5;241m1\u001b[39m,\u001b[38;5;241m5\u001b[39m,\u001b[38;5;241m6\u001b[39m]\n",
      "File \u001b[1;32mc:\\Users\\18027\\AppData\\Local\\Programs\\Python\\Python310\\lib\\unittest\\mock.py:1765\u001b[0m, in \u001b[0;36mpatch\u001b[1;34m(target, new, spec, create, spec_set, autospec, new_callable, unsafe, **kwargs)\u001b[0m\n\u001b[0;32m   1690\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mpatch\u001b[39m(\n\u001b[0;32m   1691\u001b[0m         target, new\u001b[38;5;241m=\u001b[39mDEFAULT, spec\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m, create\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m,\n\u001b[0;32m   1692\u001b[0m         spec_set\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m, autospec\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m, new_callable\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m, \u001b[38;5;241m*\u001b[39m, unsafe\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs\n\u001b[0;32m   1693\u001b[0m     ):\n\u001b[0;32m   1694\u001b[0m \u001b[38;5;250m    \u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m   1695\u001b[0m \u001b[38;5;124;03m    `patch` acts as a function decorator, class decorator or a context\u001b[39;00m\n\u001b[0;32m   1696\u001b[0m \u001b[38;5;124;03m    manager. Inside the body of the function or with statement, the `target`\u001b[39;00m\n\u001b[1;32m   (...)\u001b[0m\n\u001b[0;32m   1763\u001b[0m \u001b[38;5;124;03m    available for alternate use-cases.\u001b[39;00m\n\u001b[0;32m   1764\u001b[0m \u001b[38;5;124;03m    \"\"\"\u001b[39;00m\n\u001b[1;32m-> 1765\u001b[0m     getter, attribute \u001b[38;5;241m=\u001b[39m \u001b[43m_get_target\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtarget\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m   1766\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m _patch(\n\u001b[0;32m   1767\u001b[0m         getter, attribute, new, spec, create,\n\u001b[0;32m   1768\u001b[0m         spec_set, autospec, new_callable, kwargs, unsafe\u001b[38;5;241m=\u001b[39munsafe\n\u001b[0;32m   1769\u001b[0m     )\n",
      "File \u001b[1;32mc:\\Users\\18027\\AppData\\Local\\Programs\\Python\\Python310\\lib\\unittest\\mock.py:1606\u001b[0m, in \u001b[0;36m_get_target\u001b[1;34m(target)\u001b[0m\n\u001b[0;32m   1604\u001b[0m     target, attribute \u001b[38;5;241m=\u001b[39m target\u001b[38;5;241m.\u001b[39mrsplit(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m.\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;241m1\u001b[39m)\n\u001b[0;32m   1605\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m (\u001b[38;5;167;01mTypeError\u001b[39;00m, \u001b[38;5;167;01mValueError\u001b[39;00m, \u001b[38;5;167;01mAttributeError\u001b[39;00m):\n\u001b[1;32m-> 1606\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(\n\u001b[0;32m   1607\u001b[0m         \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mNeed a valid target to patch. You supplied: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mtarget\u001b[38;5;132;01m!r}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m   1608\u001b[0m getter \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mlambda\u001b[39;00m: _importer(target)\n\u001b[0;32m   1609\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m getter, attribute\n",
      "\u001b[1;31mTypeError\u001b[0m: Need a valid target to patch. You supplied: 'sort'"
     ]
    }
   ],
   "source": [
    "# unit test单元测试\n",
    "from unittest.mock import patch\n",
    "def sort(arr):\n",
    "    l = len(arr)\n",
    "    for i in range(0,l):\n",
    "        for j in range(i,l):\n",
    "            if arr[i] > arr[j]:\n",
    "                tmp = arr[i]\n",
    "                arr[i] = arr[j]\n",
    "                arr[j] = tmp\n",
    "@patch('sort')\n",
    "class TestSort(self,mock_sort):\n",
    "    def test_sort(self):\n",
    "        arr = [3,4,1,5,6]\n",
    "        mock_sort(arr=arr)\n",
    "        print('........')\n",
    "        self.assertEqual(arr,[1,3,4,5,6])\n",
    "# if __name__ == '__main__':\n",
    "unittest.main(argv=['first-arg-is-ignored'], exit=False)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--Return--\n",
      "None\n",
      "> \u001b[1;32mc:\\users\\18027\\appdata\\local\\temp\\ipykernel_15232\\3833372719.py\u001b[0m(5)\u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# pdb\n",
    "a = 1\n",
    "b = 2\n",
    "import pdb\n",
    "pdb.set_trace()\n",
    "c=3\n",
    "print(a+b+c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0,\n",
       " 1,\n",
       " 1,\n",
       " 2,\n",
       " 3,\n",
       " 5,\n",
       " 8,\n",
       " 13,\n",
       " 21,\n",
       " 34,\n",
       " 55,\n",
       " 89,\n",
       " 144,\n",
       " 233,\n",
       " 377,\n",
       " 610,\n",
       " 987,\n",
       " 1597,\n",
       " 2584,\n",
       " 4181,\n",
       " 6765,\n",
       " 10946,\n",
       " 17711,\n",
       " 28657,\n",
       " 46368,\n",
       " 75025,\n",
       " 121393,\n",
       " 196418,\n",
       " 317811,\n",
       " 514229,\n",
       " 832040]"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def fib(n):\n",
    "    if n == 0:\n",
    "        return 0\n",
    "    elif n == 1:\n",
    "        return 1\n",
    "    else:\n",
    "        return fib(n-1) + fib(n-2)\n",
    "\n",
    "def fib_seq(n):\n",
    "    res = []\n",
    "    if n > 0:\n",
    "        res.extend(fib_seq(n-1))\n",
    "    res.append(fib(n))\n",
    "    return res\n",
    "\n",
    "fib_seq(30)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0,\n",
       " 1,\n",
       " 1,\n",
       " 2,\n",
       " 3,\n",
       " 5,\n",
       " 8,\n",
       " 13,\n",
       " 21,\n",
       " 34,\n",
       " 55,\n",
       " 89,\n",
       " 144,\n",
       " 233,\n",
       " 377,\n",
       " 610,\n",
       " 987,\n",
       " 1597,\n",
       " 2584,\n",
       " 4181,\n",
       " 6765,\n",
       " 10946,\n",
       " 17711,\n",
       " 28657,\n",
       " 46368,\n",
       " 75025,\n",
       " 121393,\n",
       " 196418,\n",
       " 317811,\n",
       " 514229,\n",
       " 832040]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def memoize(f):\n",
    "    memo = {}\n",
    "    def helper(x):\n",
    "        if x not in memo:            \n",
    "            memo[x] = f(x)\n",
    "        return memo[x]\n",
    "    return helper\n",
    "\n",
    "@memoize\n",
    "def fib(n):\n",
    "    if n == 0:\n",
    "        return 0\n",
    "    elif n == 1:\n",
    "        return 1\n",
    "    else:\n",
    "        return fib(n-1) + fib(n-2)\n",
    "\n",
    "\n",
    "def fib_seq(n):\n",
    "    res = []\n",
    "    if n > 0:\n",
    "        res.extend(fib_seq(n-1))\n",
    "    res.append(fib(n))\n",
    "    return res\n",
    "\n",
    "fib_seq(30)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "         126 function calls (96 primitive calls) in 0.000 seconds\n",
      "\n",
      "   Ordered by: standard name\n",
      "\n",
      "   ncalls  tottime  percall  cumtime  percall filename:lineno(function)\n",
      "     31/1    0.000    0.000    0.000    0.000 2785289546.py:19(fib_seq)\n",
      "       31    0.000    0.000    0.000    0.000 2785289546.py:3(helper)\n",
      "        1    0.000    0.000    0.000    0.000 <string>:1(<module>)\n",
      "        1    0.000    0.000    0.000    0.000 {built-in method builtins.exec}\n",
      "       31    0.000    0.000    0.000    0.000 {method 'append' of 'list' objects}\n",
      "        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}\n",
      "       30    0.000    0.000    0.000    0.000 {method 'extend' of 'list' objects}\n",
      "\n",
      "\n"
     ]
    },
    {
     "ename": "",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m在当前单元格或上一个单元格中执行代码时 Kernel 崩溃。\n",
      "\u001b[1;31m请查看单元格中的代码，以确定故障的可能原因。\n",
      "\u001b[1;31m单击<a href='https://aka.ms/vscodeJupyterKernelCrash'>此处</a>了解详细信息。\n",
      "\u001b[1;31m有关更多详细信息，请查看 Jupyter <a href='command:jupyter.viewOutput'>log</a>。"
     ]
    }
   ],
   "source": [
    "import cProfile\n",
    "# def fib(n)\n",
    "# def fib_seq(n):\n",
    "cProfile.run('fib_seq(30)')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
